<?php

/**
 * Contains a class to define a User object to describe the current user.
 *
 * PHP Version 5
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */
 
/**
 * A class to define a User object to describe the current user.
 *
 * @category BuyLocal
 * @package  BuyLocal
 * @author   Kevin Fodness <kevin.fodness@gmail.com>
 * @author   Mike Pennisi <mpennisi@andrew.cmu.edu>
 * @author   Aaron Ryden <aryden@gmail.com>
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
 * @link     http://code.google.com/p/buylocalopensourcerensselaer/
 */ 
class User
{
    /**
     * The unique ID for the user from the database.
     *
     * @access public
     * @var int       Corresponds to users.id
     */
    public $id;
    
    /**
     * The user's name (e.g., John Smith).
     *
     * @access public
     * @var string    Corresponds to usernames.name
     */
    public $name;
    
    /**
     * The user's email address.
     *
     * @access public
     * @var string    Corresponds to users.email
     */
    public $email;
    
    /**
     * The user's zip code to use for radius searching.
     *
     * @access public
     * @var object    Corresponds to users.zip
     */
    public $zip;
    
    /**
     * A Role object containing information about the user's role.
     *
     * @access public
     * @var int       Corresponds to users.roleid
     */
    public $role;
    
    /**
     * A boolean flag showing whether the user's account is active or not.
     *
     * @access public
     * @var bool      Corresponds to users.isactive
     */
    public $isActive;
    
    /**
     * A UNIX timestamp for the date and time the user first registered.
     *
     * @access public
     * @var int       Corresponds to users.registeredon
     */
    public $registeredOn;
    
    /**
     * A UNIX timestamp for the date and time the user last logged on.
     *
     * @access public
     * @var int       Corresponds to users.lastlogon
     */
    public $lastLogon;
    
    /**
     * An array of Phone objects associated with this user.
     *
     * @access public
     * @var array     Corresponds to userphones where id=id
     */
    public $phoneNumbers;
    
    /**
     * The constructor function.  Builds values using parameters.
     *
     * @param int    $id           The unique ID for this user.
     * @param string $name         The user's name.
     * @param string $email        The user's email address.
     * @param object $zip          The zip code for location services.
     * @param int    $role         The user's role.
     * @param bool   $isActive     Whether the user is active or not.
     * @param int    $registeredOn The date and time of registration.
     * @param int    $lastLogon    The date and time the user last logged on.
     * @param array  $phoneNumbers An array of Phone objects for this user.
     *
     * @access public
     * @return null
     */
    public function __construct($id = null, $name = null, $email = null, 
        $zip = null, $role = 0, $isActive = 1, $registeredOn = null, 
        $lastLogon = null, $phoneNumbers = array()
    ) {
        $this->id           = $id;
        $this->name         = $name;
        $this->email        = $email;
        $this->zip          = $zip;
        $this->role         = $role;
        $this->isActive     = $isActive;
        $this->registeredOn = $registeredOn;
        $this->lastLogon    = $lastLogon;
        $this->phoneNumbers = $phoneNumbers;
    }
    
    /**
     * The clone function. Is called via the PHP5 'clone' keyword.
     * Since the 'clone' keyword automatically does a shallow copy of variables,
     * this code finishes the work and does a deep copy.
     *
     * @access public
     * @return null
     */
    public function __clone()
    {
        foreach ($this as $key => $val) {
            if (is_object($val) || is_array($val)) {
                $this->{$key} = unserialize(serialize($val));
            }
        }
    }

    /**
     * Prints a segment in the admin panel.
     *
     * @access public
     * @return null
     */
    public function printAdmin()
    {
        echo '  <li>', "\n",
            '   <div style="float:right; width: 150px;">', "\n",
            '    <a href="?action=delete&amp;user=', encode($this->id),
            '" onclick="return confirm(\'Are you sure you wish to delete this',
            ' account?\');"style="float:right">Delete</a><br />', "\n";

        if ($this->role != R_ADMIN) {
            echo '    <a href="?action=makeAdmin&amp;user=', encode($this->id),
                '" onclick="return confirm(\'Are you sure you wish to make this',
                ' user an admin?\');" style="float:right">Make An Admin</a><br />',
                "\n";
        } else {
            echo '    <a href="?action=revokeAdmin&amp;user=', encode($this->id),
                '" onclick="return confirm(\'Are you sure you wish to revoke this',
                ' admin?\');" style="float:right">Revoke Admin</a><br />', "\n";
        }
        
        echo '   </div>', "\n",
            '   <div>', "\n",
            '    <h2>';
        echo encode($this->name);
        echo '</h2>', "\n",
            '    <address>', "\n",
            $this->zip->zip, '<br/>', "\n",
            '    </address>', "\n",
            '    <p>', encode($this->email), '</p>', "\n";
        $this->_printPhoneNumbers();
        echo '   </div>', "\n",
            '  </li>', "\n";
    }

    /**
     * Function to print a list of phone numbers for this store.
     *
     * @access private
     * @return null
     */
    private function _printPhoneNumbers()
    {
        if (count($this->phoneNumbers) == 0) {
            return null;
        }
        echo '    <p><strong>Phone Numbers:</strong></p>', "\n",
            '    <ul>', "\n";
        foreach ($this->phoneNumbers as $phone) {
            echo '     <li><strong>', $phone->description, ':</strong> ',
                $phone->number, '</li>', "\n";
        }
        echo '    </ul>', "\n";
    }
}

?>